package com.atguigu.tingshu.album.api;

import cn.hutool.core.collection.CollUtil;
import com.atguigu.tingshu.album.mapper.TrackInfoMapper;
import com.atguigu.tingshu.album.service.AuditService;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

@Slf4j
@Component
public class ReviewTask {
    @Autowired
    private TrackInfoMapper trackInfoMapper;
    @Autowired
    private AuditService auditService;

    /**
     * @定时任务: 获取一次审核任务状态结果/5s
     * */
    @Scheduled(cron = "0/30 * * * * ?")
    public void checkReviewStatusTask() {
        //获取正在审核中状态媒体资源列表
        List<TrackInfo> trackInfoList = trackInfoMapper.selectList(new LambdaQueryWrapper<TrackInfo>()
                .eq(TrackInfo::getStatus, SystemConstant.TRACK_STATUS_REVIEWING)
                .select(TrackInfo::getId, TrackInfo::getReviewTaskId)
                .last("limit 100")
        );
        if (CollUtil.isNotEmpty(trackInfoList)) {
            for (TrackInfo trackInfo : trackInfoList) {
               String suggestion = auditService.getReviewStatus(trackInfo.getReviewTaskId());
                if (StringUtils.isNotEmpty(suggestion)){
                    //更新审核状态
                    if ("pass".equals(suggestion)) {
                        trackInfo.setStatus(SystemConstant.TRACK_STATUS_PASS);
                    } else if ("block".equals(suggestion)) {
                        trackInfo.setStatus(SystemConstant.TRACK_STATUS_NO_PASS);
                    } else if ("review".equals(suggestion)) {
                        trackInfo.setStatus(SystemConstant.TRACK_STATUS_ARTIFICIAL);
                    }
                    trackInfoMapper.updateById(trackInfo);
                }
            }
        }
    }
}
